package com.tbit.uqbike.client.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:28 2019/12/27
 */
@Slf4j
public class ObjectUtil {


    /**
     * 根据属性名获取属性值
     */
    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }


    /**
     * 生成拍照参数
     *
     * @param uploadNow    是否立即上报 bit0
     *                     0：不上传       1：直接上传
     * @param qualityLevel 像素等级 bit1-2
     *                     0：低质量（默认）  1：中质量  2：高质量
     * @param cameraNo     摄像头编号 bit3-5
     *                     0：逆行，闯红灯，非机动车道
     *                     1：识别头盔，多人骑行
     *                     2：换车识别，站点角度识别
     * @return
     */
    public static String buildPhotoParam(int uploadNow, int qualityLevel, int cameraNo) {
        Integer i = 0;
        i |= cameraNo;
        i = i << 2;
        i |= qualityLevel;
        i = i << 1;
        i |= uploadNow;
        return i.toString();
    }

    public static boolean statusParse(Integer n, Integer terStatus) {
        return ((1 << n) & terStatus) != 0;
    }

    /**
     * mac转化
     *
     * @param mac
     * @return
     */
    public static String macCheck(String mac) {
        StringBuffer stringBuffer = new StringBuffer();
        char[] chars = mac.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            stringBuffer.append(chars[i]);
            if (i % 2 != 0) {
                stringBuffer.append(":");
            }
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());
        return stringBuffer.toString();
    }

    /**
     * 获取位数构造数据
     *
     * @param content
     * @param num
     * @return
     */
    public static long buildTlv(Long content, Integer num) {
        long t = (1 << num) | content;
        return t;
    }

    /**
     * hex转二进制
     *
     * @param hex
     * @return
     */
    public static String hexToBin(String hex) {
        String bin = "";
        String binFragment = "";
        int iHex;
        hex = hex.trim();
        hex = hex.replaceFirst("0x", "");
        for (int i = 0; i < hex.length(); i++) {
            iHex = Integer.parseInt("" + hex.charAt(i), 16);
            binFragment = Integer.toBinaryString(iHex);
            while (binFragment.length() < 4) {
                binFragment = "0" + binFragment;
            }
            bin += binFragment;
        }
        return bin;
    }

    public static void main(String[] args) throws DecoderException {
        boolean b = statusParse(11, 7820);
        boolean b1 = statusParse(2, 6);
        System.out.println(b);
        System.out.println(b1);
        System.out.println("======");
        StringBuffer stringBuffer = new StringBuffer(hexToBin("110430"));
        System.out.println(hexToBin("110430"));

        System.out.println(StringUtil.getIndex("110430", 20));
        System.out.println(StringUtil.getIndex("110430", 13));
        System.out.println(statusParse(1, 6));
    /*System.out.println(stringBuffer.reverse().substring(12,13));
    System.out.println(stringBuffer.reverse().substring(19,20));
    int  decimal=Integer.parseInt(hexToBin("110430"), 2 );
    System.out.println(decimal);*/
    /*Long i =buildTlv(0L,11);
    System.out.println(i);
    System.out.println(statusParse(11,0));
    System.out.println(statusParse(11,i.intValue()));
    System.out.println(statusParse(4,8));
    System.out.println(statusParse(5,8));*/
    /*String string=null;
    Integer model = string==null?0:Integer.valueOf(string);

    System.out.println(true==true);*/
    /*SortedMap<Object,Object> tree = new TreeMap<>();
    tree.put("mch_id", "1583642381");
    tree.put("service_id", "00002001000000159824267667291733");
    tree.put("out_request_no", "WXZFF202010121515050001");
    tree.put("timestamp","1602486905029");
    tree.put("nonce_str", "J2Yu9h2B8ifAMSEg6581Y4i5H0kbVqZ3");
    tree.put("sign_type","HMAC-SHA256");
    String sign = HMACSHA256.sha256_HMAC(tree, "xjcx74123CLIENT36987tbit963258ww");
    System.out.println(sign);
    tree.put("sign",sign);*/

        // System.out.println(0x3D);

    }

    /**
     * 获取属性名数组
     */
    private String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     */
    private List getFiledsInfo(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        List list = new ArrayList();
        Map infoMap = null;
        for (int i = 0; i < fields.length; i++) {
            infoMap = new HashMap();
            infoMap.put("type", fields[i].getType().toString());
            infoMap.put("name", fields[i].getName());
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     */
    public Object[] getFiledValues(Object o) {
        String[] fieldNames = this.getFiledName(o);
        Object[] value = new Object[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++) {
            value[i] = getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }


}
